package mqq.controller;

import java.awt.event.ActionEvent;

import mqq.model.DecisionLinesEvent;
import mqq.view.ApplicationPanel;

/**
 * Controller to manage the choice text fields
 * @author mqq
 */
public class ChoiceController {
	/** Line position responsible for */
	int position;
	
	/** Boundary object responsible for */
	ApplicationPanel view;

	/** Entity object responsible for */
	DecisionLinesEvent event;
	
	/** React to changes by updating given entity. */
	public ChoiceController (DecisionLinesEvent event, int position, ApplicationPanel app) {
		this.event = event;
		this.position = position;
		this.view = app;
	}
	
	/**
	 * Respond to attempts to change the text field.
	 * @param ae  Action that the user took
	 */
	public void actionPerformed(ActionEvent ae) {
		if (view.getChoiceActive()) {
			String s = view.getChoiceTextField(position).getText();
			if (event.setChoice(position, s)) {
				view.getChoiceTextField(position).setText(s);
				view.setWarning("");
			} else {
				view.setWarning("Invalid choice input. Choice remain unchanged. Please try again.");
			}
		}
	}
}
